package com.classeven.downhill.state;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.modifier.RotationAtModifier;
import org.andengine.entity.modifier.RotationByModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.modifier.IModifier;

import android.view.KeyEvent;

import com.classeven.downhill.define.iGameDefines;
import com.classeven.downhill.element.AScene;
import com.classeven.downhill.element.ASprite;
import com.classeven.downhill.engine.GameEngine;
import com.classeven.downhill.engine.Utils;

public class State_GamePause {

	ASprite bg_pause, btn_menu, btn_restart, btn_play, effect;
	GameEngine engine;
	AScene scene;
	
	int rot;
	
	public State_GamePause(GameEngine engine)
	{
		this.engine = engine;
		bg_pause = new ASprite(engine, iGameDefines.SPRITE_PAUSE, iGameDefines.PAUSE_BG);
		btn_menu = new ASprite(engine, iGameDefines.SPRITE_PAUSE, 1);		
		btn_restart = new ASprite(engine, iGameDefines.SPRITE_PAUSE, 2);
		btn_play = new ASprite(engine, iGameDefines.SPRITE_PAUSE, 3);
		effect = new ASprite(engine, iGameDefines.SPRITE_PAUSE, iGameDefines.PAUSE_EFFECT);
	}

	public void init()
	{
		setPos();
		engine.SceneAttach(State_GamePlay.scene, bg_pause);
		engine.SceneAttach(State_GamePlay.scene, effect);
		
		engine.SceneAttach(State_GamePlay.scene, btn_menu);
		engine.SceneAttach(State_GamePlay.scene, btn_play);
		engine.SceneAttach(State_GamePlay.scene, btn_restart);
		
		State_GamePlay.scene.theScene().registerTouchArea(btn_menu.theSprite());
	}
	
	public void update()
	{
	
		rot++;
		if(rot ==360)
			rot =0;

		effect.theSprite().setRotation(rot);
				
	}
	
	public void exit()
	{
		bg_pause.DetachSelf();
		btn_play.DetachSelf();
		btn_menu.DetachSelf();
		btn_restart.DetachSelf();
		effect.DetachSelf();
	}

	private void setPos()
	{
		float y = bg_pause.GetY()  + bg_pause.theSprite().getHeight() - (btn_menu.theSprite().getHeight() + Utils.GetRatioHeight(50)) ;
		float margin = btn_menu.theSprite().getWidth() / 2;
		btn_menu.SetPos(bg_pause.GetX() + margin , y);
		btn_play.SetPos(Utils.getCenterObjX(bg_pause.theSprite(), btn_play.theSprite()), y);
		btn_restart.SetPos(bg_pause.GetX() + bg_pause.theSprite().getWidth() -(3 * margin) , y);
	}
	
	public void back(int keyCode, KeyEvent event) 
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			exit();
			engine.SetNextGameState(iGameDefines.GAME_STATE_GAME_PLAY);
		}
		
	}

	public void event(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		
		if(pTouchArea.equals(btn_menu.theSprite()) && pSceneTouchEvent.isActionDown())
		{
			exit();
			State_GamePlay.scene.theScene().detachChildren();
			engine.SetNextMessageState(0);
			engine.SetNextGameState(iGameDefines.GAME_STATE_MAIN_MENU);
		}
	}
	
	
}

